// pages/Index/bill/bill.js
const controllers = require('../../controllers/controllerIndex').controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billList: [
      // {price:1,name:2},{price:1,name:3},{price:1,name:4}
    ],
    time: '',
    shopId: '',
    choiceactive: 0,
    way: 0,
    endTime: ''
  },
  //选择方式
  choice(e) {
    this.setData({
      choiceactive: e.currentTarget.dataset.index,
      way: e.currentTarget.id
    })
  },
  //提交续费订单
  ok() {
    controllers.comfirmBill({
      renewId: this.data.way,
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      let orderNo = res.data.orderNo
      let accountId=res.data.accountId
      let orderId=res.data.id
      // controllers.paySucess({
      //   shopId: wx.getStorageSync('shopId'),
      //   orderId: orderId,
      //   accountId: accountId,
      //   orderNo: orderNo,
      //   transId: 1
      // }).then(res => {
      //   wx.showToast({
      //     title: '成功',
      //     icon: 'none',
      //     // success: function () {
      //     //   wx.navigateTo({
      //     //     url: '/pages/Index/Index',
      //     //   })
      //     // }
      //   })
      //   setTimeout(function(){
      //       wx.navigateTo({
      //         url: '/pages/Index/Index',
      //       })
      //   },1000)
      // })
      controllers.weChatPay({
        shopId: wx.getStorageSync('shopId'),
        orderNo:orderNo,
        frontUrl:"mp:///pages/Index/Index",
        type:"SP",
        openId:wx.getStorageSync('openId')
      }).then(res =>{
        console.log(res);
        // wx.navigateTo({
        //   url: '/pages/weixinlink/weixinlink?url='+encodeURIComponent(res.data)
        // })
        let data=JSON.parse(res.data);
        console.log(data)
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success(res){
            console.log(res)
          },
          fail(res){
            console.log(res)
          }
        })
      })

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      endTime: wx.getStorageSync('endTime').slice(0,10)
    })
    //获取续费列表
    controllers.getBillList().then(res => {
      this.setData({
        billList: res.data,
        way: res.data[0].id
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})